#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

static int cb_select_stu(void *NotUsed,int argc, char **argv,char **azColName) {
    int i;
    for(i = 0; i < argc; i++) {
        printf("%s = %s\n", azColName[i], argv[i]);
    }
    printf("\n");
    return 0;
}

static int cb_create_table(void *NotUsed,int argc, char **argv,char **azColName) {
    int i;
    for(i = 0; i < argc; i++) {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int main(int argc, char* argv[]) {
    sqlite3 *db;
    char *err = 0;
    char *sql;
    int rc;

    rc = sqlite3_open("./test.db", &db);
    if(rc) {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_free(err);
        exit(1);
    }

    //查询数据
    sql = "select * from stu";
    rc = sqlite3_exec(db, sql, cb_select_stu, 0, &err);
    if(rc != SQLITE_OK) {
        fprintf(stderr, "SQL err: %s\n", err);
        sqlite3_free(err);
        exit(1);
    }
    printf("select stu ok\n");

    //创建表
    sql = "create table if not exists jwstu(name char[10], sex char[10], num int, score int);";
    rc = sqlite3_exec(db, sql, cb_create_table, 0, &err);
    if(rc != SQLITE_OK) {
        fprintf(stderr, "SQL err: %s\n", err);
        sqlite3_free(err);
        exit(1);
    }
    printf("create table jwstu ok\n");

    sql = "insert into jwstu values(\"jwtester\", \"nan\", 30, 100);";
    rc = sqlite3_exec(db, sql, cb_create_table, 0, &err);
    if(rc != SQLITE_OK) {
        fprintf(stderr, "SQL err: %s\n", err);
        sqlite3_free(err);
        exit(1);
    }
    printf("insert into jwstu ok\n");

    sqlite3_close(db);
    return 0;
}

